/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    serverActions: {
      bodySizeLimit: "10mb",
    },
  },
  webpack: (config, { isServer }) => {
    if (!isServer) {
      // Handle node: protocol imports in client-side code
      config.resolve.fallback = {
        ...config.resolve.fallback,
        "async_hooks": false,
        "node:async_hooks": false,
        "fs": false,
        "node:fs": false,
        "path": false,
        "node:path": false,
        "os": false,
        "node:os": false,
        "crypto": false,
        "node:crypto": false,
        "stream": false,
        "node:stream": false,
        "util": false,
        "node:util": false,
        "buffer": false,
        "node:buffer": false,
        "events": false,
        "node:events": false,
      };
      
      // Add alias for node: protocol
      config.resolve.alias = {
        ...config.resolve.alias,
        "node:async_hooks": false,
        "node:fs": false,
        "node:path": false,
        "node:os": false,
        "node:crypto": false,
        "node:stream": false,
        "node:util": false,
        "node:buffer": false,
        "node:events": false,
      };
      
      // Add externals to provide mock implementations
      config.externals = {
        ...config.externals,
        "async_hooks": "{ AsyncLocalStorage: function() { this.getStore = () => undefined; this.run = (store, fn) => fn(); this.enterWith = () => {}; } }",
        "node:async_hooks": "{ AsyncLocalStorage: function() { this.getStore = () => undefined; this.run = (store, fn) => fn(); this.enterWith = () => {}; } }",
        "fs": "{}",
        "node:fs": "{}",
        "path": "{}",
        "node:path": "{}",
        "os": "{}",
        "node:os": "{}",
        "crypto": "{}",
        "node:crypto": "{}",
        "stream": "{}",
        "node:stream": "{}",
        "util": "{}",
        "node:util": "{}",
        "buffer": "{}",
        "node:buffer": "{}",
        "events": "{}",
        "node:events": "{}",
      };
    }
    return config;
  },
};

export default nextConfig;
